<?php include ROOT . 'vendor/autoload.php';

/**
 * 导入类
 */
use Elasticsearch\ClientBuilder;

/**
 * ElasticSearch操作类
 */
class Edb {

     /**
      * 索引
      *
      * @var string
      */
    public $index = '';

    /**
     * 类型
     *
     * @var string
     */
    public $type = '';

    /**
     * es实例
     *
     * @var object
     */
    public $client = null;

    /**
     * 主机地址
     *
     * @var array
     */
    public $host;

    /**
     * 初始化
     *
     * @param array $config 配置数组
     */
    function __construct($config) {
        if(!isset($config['index'])) {
            throw new InvalidConfigException("请填写索引");
        }
        if(!isset($config['type'])) {
            throw new InvalidConfigException("请填写类型");
        }
        if(!isset($config['host'])) {
            throw new InvalidConfigException("请填写主机");
        }
        if(!is_array($config['host'])) {
            throw new InvalidConfigException("主机必须是数组");
        }   
        $this->index = $config['index'];
        $this->type = $config['type'];
        $this->client = ClientBuilder::create()->setHosts($config['host'])->build();

    }

    /**
     * 删除索引
     * 
     * @return object
     */
    public function deleteIndex() {
        $data = [
            "index" => $this->index,
        ];
        $res = $this->client->indices()->delete($data);
        return $res;
    }

    /**
     * 批量插入
     *
     * @param array $params 要插入的字段信息
     * @return object
     */
    public function postBulkDoc(array $params) {
        $data = ['body' => []];
        for($i=0; $i<count($params); $i++) {

            // 如果参数带id
            if(isset($params['id'])) {
                $data['body'][] = [
                    "index" => [
                        '_index' => $this->index,
                        '_type' => $this->type,
                        '_id' => $params[$i]['id']
                    ]
                ];    
            }
            
            // 
            else {
                $data['body'][] = [
                    "index" => [
                        '_index' => $this->index,
                        '_type' => $this->type,
                    ]
                ];    
            }            
            $data['body'][] = $params[$i];
        }
        $res = $this->client->bulk($data);
        return $res;
    }   

     /**
     * 单条插入
     *
     * @param array $params 插入的字段
     * @return object
     */
    public function insertDoc($params) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "body" => $params
        ];
        if(isset($params['id'])){
            $data['id'] = $params['id'];
        }
        $res = $this->client->index($data);
        return $res;
    }

     /**
     * 获取单条信息
     *
     * @param $id 数据id
     */
    public function getDocById($id) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "id" => $id
        ];
        $res = $this->client->get($data);
        return $res;
    }

     /**
     * 更新单条数据
     *
     * @param $id 要更新数据的id
     * @param $params  要更新的字段信息
     * @return object
     */
    public function updateDocById($id,array $params) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "id" => $id,
            "body" => [
                'doc' => $params
            ]
        ];
        $res = $this->client->update($data);
        return $res;
    }

    /**
     * 批量更新
     *
     * @param array $params 要更新的字段信息
     * @return object
     */
    public function updateBulkDoc(array $params) {
        $data = ['body' => []];
        for($i=0; $i<count($params); $i++) {

            // 
            if(isset($params[$i]['id'])) {
                $data['body'][] = [
                    "update" => [
                        '_index' => $this->index,
                        '_type' => $this->type,
                        '_id' => $params[$i]['id']
                    ]
                ];    
            }
            
            // 
            else {
                $data['body'][] = [
                    "update" => [
                        '_index' => $this->index,
                        '_type' => $this->type,
                    ]
                ];    
            }
            $data['body'][] = ['doc' => $params[$i]];
        } 
        $res = $this->client->bulk($data);
        return $res;
    }   

     /**
     * 删除单条信息
     *
     * @param  $id 数据id
     * @return object
     */
    public function deleteDocById($id) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "id" => $id
        ];
        $res = $this->client->delete($data);
        return $res;
    }

     /**
     * 常规查询/搜索
     *
     * @param array $params elasticsearch查询语法
     * @return object
     */
    public function searchDoc($params) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "body" => $params
        ];
        $res = $this->client->search($data);
        return $res;
    }

    /**
     * 滚动查询/搜索
     * 可以查询全部数据（效率很慢）
     *
     * @param array $query elasticsearch查询语法
     * @return object
     */
    public function search($query) {
        $res = $this->client->search($query);
        return $res;
    }
    

     /**
     * 滚动查询/搜索  配合search方法使用
     *
     * @param $scroll_id  滚动id
     * @param $second   多长时间开始滚动
     * @return object
     */
    public function scroll($scroll_id,$second) {
        $data = [
            "scroll_id" => $scroll_id,
            "scroll" => $second
        ];
        $res = $this->client->scroll($data);
        return $res;
    }

    /**
     * 查询数据的总条数
     *
     * @param array $params elasticsearch查询语法
     * @return object
     */
    public function count(array $params) {
        $data = [
            'index' => $this->index,
            "type" => $this->type,
            "body" => $params
        ];
        $res = $this->client->count($data);
        return $res;
    }
}
